<template>
	<view class="container">
		<view class="">
			<image class="avatar" src="../../static/images/fish.png" mode=""></image>
		</view>
		<uni-forms ref="form" :model="user" :rules="rules">
			<uni-forms-item label="用户名" name="name">
				<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
			</uni-forms-item>

			<uni-forms-item label="密码" name="password">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>

			<uni-forms-item label="">
				<button type="primary" @click="submit">登录</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: 'zzz',
					password: '123',
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}]
					},
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								minLength: 3,
								maxLength: 5,
								errorMessage: '密码长度在 3 到 5 个字符之间',
							}
						]
					}
				},
			}
		},
		methods: {
			submit(e) {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						console.log(res)
						if (res.errMsg === 'request:ok') {
							uni.showToast({
								title: '登录成功'
							});
							uni.setStorageSync('userinfo', res.data.data)
							uni.switchTab({
								url: './my'
							})
						}else{
							uni.showToast({
								title: '用户名或密码错误',
								icon:'error'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon:'error'
						});
					},
					complete: () => {}
				});
			},
		}
	}
</script>

<style scoped>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}

	.avatar {
		width: 100px;
		height: 100px;
		margin-left: 230rpx;
		margin-bottom: 20px;
	}
</style>